/**  
* <p>Title: RestTemplateConfig.java</p>  
* <p>Description: </p>  
* <p>Copyright: Copyright (c) 2017</p>  
* <p>Company: http://www.richfit.com/</p>  
* @author Administrator
* @date 2020年3月12日  
* @version 1.0  
*/  
package com.rf.richfitwheel.admin.conf;

import java.nio.charset.Charset;
import java.util.Collections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;


/**  
* RestTemplate客户端初始化
* @author 胡寿全  
* @date 2020年3月12日  
*/
@Configuration
public class RestTemplateConfig {
	@Autowired
	private RestTemplateTokenInterceptor restTemplateTokenInterceptor;
	// 配置 RestTemplate
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
    	RestTemplate restTemplate = new RestTemplate(factory);
    	restTemplate.setInterceptors(Collections.singletonList(restTemplateTokenInterceptor));
        return restTemplate;
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        // 创建一个 httpCilent 简单工厂
    	HttpsClientRequestFactory factory = new HttpsClientRequestFactory();
        // 设置连接超时
        factory.setConnectTimeout(300*1000);
        // 设置读取超时
        factory.setReadTimeout(300*1000);
        return factory;
    }
    
    /**
     * 这个是用来调用外部接口的resttemplate
     * @param factory
     * @return
     */
    @Bean
    public RestTemplate restTemplateOuter(ClientHttpRequestFactory factory){
    	RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
//    	restTemplate.setInterceptors(Collections.singletonList(restTemplateTokenInterceptor));
        return restTemplate;
    }
}
